package personal.web.mvc.controller.accounting;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import personal.domain.accounting.WeeklyBalance;
import personal.exception.UnauthorizedAccessException;
import personal.service.accounting.WeeklyBalanceService;
import personal.web.mvc.ControllerExceptionHandler;
import personal.web.mvc.controller.AbstractCommandController;
import personal.web.mvc.io.accounting.AccountingRequestParam;


@Controller("/weeklybalance")
public class WeeklyBalanceController  extends AbstractCommandController{
	private WeeklyBalanceService weeklyBalanceService;
	private ControllerExceptionHandler controllerExceptionHandler;

	public WeeklyBalanceService getWeeklyBalanceService() {
		return weeklyBalanceService;
	}

	@Autowired(required=false)
	public void setWeeklyBalanceService(WeeklyBalanceService weeklyBalanceService) {
		this.weeklyBalanceService = weeklyBalanceService;
	}

	public ControllerExceptionHandler getControllerExceptionHandler() {
		return controllerExceptionHandler;
	}

	@Autowired(required=false)
	public void setControllerExceptionHandler(ControllerExceptionHandler controllerExceptionHandler) {
		this.controllerExceptionHandler = controllerExceptionHandler;
	}
	
	@ExceptionHandler(RuntimeException.class)
	public void handleException(final RuntimeException e, final HttpServletResponse response) {
		controllerExceptionHandler.handleRuntimeException(e, response);
	}
	
	@ExceptionHandler(UnauthorizedAccessException.class)
	public void handleException(final UnauthorizedAccessException e, final HttpServletResponse response) {
		controllerExceptionHandler.handleRuntimeException(e, response);
	}

	@RequestMapping(value="/ajax/loadall",method=RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> loadWeeklyBalances(@RequestBody AccountingRequestParam param, HttpServletResponse httpResponse) {
		Map<String, Object> response = getResponseMap();
		response.put("weeklyBalances", weeklyBalanceService.getAllWeeklyBalances());
		return response;
	}

	@RequestMapping(value="/ajax/updatelist",method=RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> updateWeeklyBalances(@RequestBody AccountingRequestParam param, HttpServletResponse httpResponse) {
		Map<String, List<WeeklyBalance>> weeklyBalanceUpdates = param.getWeeklyBalanceUpdates();
		if (weeklyBalanceUpdates.containsKey("savelist")) {
			weeklyBalanceService.saveWeeklyBalances(weeklyBalanceUpdates.get("savelist"));
		}
		if (weeklyBalanceUpdates.containsKey("removelist")) {
			List<String> descriptions = new ArrayList<String>();
			for(int i = 0; i < weeklyBalanceUpdates.get("removelist").size(); i++) {
				descriptions.add(weeklyBalanceUpdates.get("removelist").get(i).getDescription());
			}
			weeklyBalanceService.deleteWeeklyBalances(descriptions);
		}
		return getResponseMap();
	}
}
